<template>
	<view class="container" >
		<view class="status_bar">
			<view style="margin-left: 4%;" @tap="goBack()"><u-icon name="arrow-leftward" size='24' /></view>
			<view style="margin-left: -50upx; font-weight: bold;">动态详情</view>
			<view @tap="updateMask = true" class="right">修改</view>
			</u-icon>
		</view>
		<view class="scroll-view-box">
			<scroll-view scroll-y class="list">
				<!-- <view class="space" @tap="closeSwipe"></view> -->
				<uni-swipe-action ref="swipe">
					<!-- v-for="(item, index) in list" :key="index" -->
					<uni-swipe-action-item >
						<view class="u-page">
							<view class="u-demo-block">
								<view class="u-demo-block__content">
									<view class="album">
										<view class="album__avatar">
											<view style="border-radius: 30upx;" v-if="info.avatar">
												<u--image :showLoading="true" :src="info.avatar" width="110upx"
													height="130upx" @tap="click" radius="10upx"></u--image>
											</view>
											<view v-else class="name-img"></view>
										</view>
										<view class="album__content">
											<view>
												<u--text :text="info.create_time" type="primary" bold
													size="17"></u--text>
												<view>{{info.name}}</view>
											</view>
											<u-icon name="trash" size='30' v-if="info.user_id == userId" color="#ccdee8"
												@click="delcircle(info)"></u-icon>
										</view>
									</view>
									<view class="album-container">
										<u--text margin="0 0 8px 0" :text="info.content"></u--text>
										<u-album :urls="info.pictures" multipleSize="100"></u-album>
									</view>
									<view class="icon-box">
										
										<u-icon v-if="info.liked" name="thumb-up-fill" size='26' color="#086af3"
											style="margin-right: 20upx;" @tap="handleLike(info)"></u-icon>
										<u-icon v-else name="thumb-up" size='26' color="#ccdee8"
											style="margin-right: 20upx;" @tap="handleLike1(info)"></u-icon>
											<text v-if="info.liked" style="margin-right: 15px;margin-left: -5px;">{{info.like_count}}</text>
										<!-- <u--text v-if="item.liked" :text="item.like_count"></u--text> -->
										<!-- <u-icon v-if="commentShow" name="chat" size='26' color="#ccdee8"
											style="margin-right: 20upx;" @tap="handleSpesk()"></u-icon>
										<u-icon v-else name="chat-fill" size='26' color="#086af3"
											style="margin-right: 20upx;"></u-icon> -->
										<u-icon v-if="info.commentShow" name="chat-fill" size='26' color="#086af3"
											style="margin-right: 20upx;"></u-icon>
										<u-icon v-else name="chat" size='26' color="#ccdee8"
											style="margin-right: 20upx;" @tap="handleSpesk(info)"></u-icon>
				
				
									</view>
									<view v-if="info.user_id == userId &&  info.comment_list.length > 0" class="speak-box">
										<view v-for="item_c in info.comment_list" :key="item_c.id" style="display: flex;">
											<span style="color: #1e70d3;">{{item_c.reply_name}}</span>:{{item_c.content}}
											
											<u-icon name="trash" size='20' v-if="item_c.user_id == userId" color="#ccdee8"
												@click="delcomment(item_c,item.id)" style="margin-left: 20px;"></u-icon>
										</view>
				
									</view>
				
									<view v-else-if="valueSpeak1" class="speak-box"><span
											style="color: #1e70d3;">{{$util.getUsername()}}</span>:{{valueSpeak}}</view>
									<view v-if="info.commentShow" style="position: relative;">
										<u--input placeholder="说点什么吧" border="surround" v-model="valueSpeak"
											@confirm="confirmSpeak">
											<!-- <template slot="suffix">
												<u--image class="btn-enter" :showLoading="true"
													src="../../static/image/btn-enter@3x.png" width="40upx" height="40upx"
													@tap="confirmSpeak" radius="10upx"></u--image>
											</template> -->
										</u--input>
										<!-- <u--image class="btn-enter" :showLoading="true"
											src="../../static/image/btn-enter@3x.png" width="40upx" height="40upx"
											@tap="click" radius="10upx"></u--image> -->
									</view>
								</view>
							</view>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</scroll-view>
		</view>


		<view style="height: 400upx;">
			<u-popup :show="updateMask" @close="closeMask" @open="openMask" mode="bottom" :customStyle="customStyle"
				round="20upx" closeOnClickOverlay="true">
				<view class="con-shop">
					<view style="display: flex;">
						<view style="margin-top: 10upx;color: #313131;">展示范围</view>
					</view>
					<view style="display: flex;">
						<view style="margin-right: 20upx;">陌生人可见</view>
						<u-switch v-model="valueScope" @change="change" size=20 activeColor="#1ea7dd"
							inactiveColor="#ececec"></u-switch>
					</view>
				</view>
				<view class="mask-btn">
					<view class="mask-btn-left" @tap="updateMask=false">取消</view>
					<view class="mask-btn-right" @click="update">确认修改</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userId') || '',
				startX: 0, // 触摸开始的X坐标
				endX: 0, // 触摸结束的X坐标
				threshold: 50, // 设置滑动多少距离后触发返回
				//urlList: 'https://cdn.uviewui.com/uview/album/1.jpg',
				post_id:'',
				urlList: '',
				likeShow: true,
				commentShow: false,
				valueSpeak: '',
				valueSpeak1: '',
				list:[],
				info:{},
				customStyle: {
					hight: "400upx"
				},
				valueScope:false,
				updateMask: false,
				list1: [{
						name: "哈哈哈",
						time: "09:52",
						con: '纳斯达克就会放开手',
						faceUrl: '',
						conUrl: ''
					}
				],
				albumWidth: 0,
				urls2: [
					'https://cdn.uviewui.com/uview/album/1.jpg',
					'https://cdn.uviewui.com/uview/album/2.jpg',
					'https://cdn.uviewui.com/uview/album/3.jpg',
					'https://cdn.uviewui.com/uview/album/4.jpg',
					'https://cdn.uviewui.com/uview/album/5.jpg',
					'https://cdn.uviewui.com/uview/album/6.jpg',
					'https://cdn.uviewui.com/uview/album/7.jpg',
					'https://cdn.uviewui.com/uview/album/8.jpg',
					'https://cdn.uviewui.com/uview/album/9.jpg',
					'https://cdn.uviewui.com/uview/album/10.jpg',
				],
			}
		},
		onLoad(option) {
			console.log(option);
			// this.post_id = option.id
			// this.info = option.info
			// this.getlist()
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('circleInfo',(params) => {
				console.log(params);
				this.info = params.data;
				this.valueScope = Boolean(this.info.is_private);
				
				
			})
		},
		methods: {
			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},
			
			
			change(e){
				console.log('change', e)
				this.valueScope = e;
			},
			openMask() {
				// console.log('open');
			},
			closeMask() {
				this.updateMask = false
				// console.log('close');
			},
			getlist() {
				
				
				uni.request({
					url: this.$api.baseUrl + '/api/common/post/fetch',
					header: {
						'content-type': 'application/json'
					},
					data: {
						page: this.page,
						page_size: this.pageSize,
						user_id: uni.getStorageSync('userId'),
						post_id:this.post_id
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						if (res.data.code == 0) {
							if (this.page == 1) {
								this.list = [];
							}
			
							for (let i = 0; i < res.data.data.length; i++) {
								let list_item = res.data.data[i];
								
								this.list.push(list_item);
							}
							if (res.data.data.length >= this.pageSize) {
								this.hasMore = true;
							} else {
								this.hasMore = false;
							}
						}
			
			
			
						console.log(this.list);
						// this.list = res.data;
					},
					fail: res => {
						console.log(res);
					}
			
				})
			},
			handleLike(item) {
				// let index = this.list.findIndex(s_item => s_item.id == item.id)
				// if (index >= 0) {
				// 	this.list[index].likeShow = false
				// }
				let like_id = item.id;
				console.log(like_id)
				uni.request({
					url: this.$api.baseUrl + '/api/common/post_like/delete',
					header: {
						'content-type': 'application/json'
					}, // this.$util.getHeader(),
					data: {
						post_id: item.id,
						user_id: uni.getStorageSync('userId')
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						// this.list = res.data;
						if(res.data.code == 0){
							let index = this.list.findIndex(s_item => s_item.id == item.id)
							if (index >= 0) {
								this.list[index].liked = ''
								this.list[index].like_count = 0
							}
						}
					},
					fail: res => {
						console.log(res);
					}
			
				})
			
				this.$forceUpdate();
			},
			handleLike1(item) {
				// item.likeShow = true
				// let index = this.list.findIndex(s_item => s_item.id == item.id)
				// if (index >= 0) {
				// 	this.list[index].likeShow = true
				// }
				uni.request({
					url: this.$api.baseUrl + '/api/common/post_like/add',
					header: {
						'content-type': 'application/json'
					},
					data: {
						post_id: item.id,
						user_id: uni.getStorageSync('userId')
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						// this.list = res.data;
						if(res.data.code == 0){
							let index = this.list.findIndex(s_item => s_item.id == item.id)
							if (index >= 0) {
								this.list[index].liked = item.id
								this.list[index].like_count = 1
							}
						}
					},
					fail: res => {
						console.log(res);
					}
			
				})
				this.$forceUpdate();
			},
			handleSpesk(item) {
				this.comment_info = item;
				let index = this.list.findIndex(s_item => s_item.id == item.id)
				if (index >= 0) {
					this.list[index].commentShow = true
				}
				this.$forceUpdate();
			},
			getlike(item) {
				console.log('getlike');
				return new Promise((resolve, reject) => {
					uni.request({
						url: this.$api.baseUrl + '/api/common/post_like/fetch',
						header: {
							'content-type': 'application/json'
						},
						data: {
							post_id: item.id,
							user_id: uni.getStorageSync('userId')
						},
						method: 'POST',
						success: res => {
							console.log(res.data.data);
							uni.hideLoading()
							// if (res.data.code == 0) {
							resolve(res.data.data)
							// }
						},
						fail: res => {
							console.log(res);
						}
			
					})
				})
			},
			getcomment() {
				// return new Promise((resolve, reject) => {
				uni.request({
					url: this.$api.baseUrl + '/api/common/post_comment/fetch',
					header: {
						'content-type': 'application/json'
					},
					data: {
						post_id: this.comment_info.id,
						user_id: uni.getStorageSync('userId')
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						if (res.data.code == 0) {
							// resolve(res.data.data)
			
							let index = this.list.findIndex(s_item => s_item.id == this.comment_info.id)
							console.log(index);
							console.log(res.data.data);
							if (index >= 0) {
								this.list[index].comment_list = res.data.data
								this.list[index].commentShow = false;
							}
			
							this.$forceUpdate();
						}
					},
					fail: res => {
						console.log(res);
					}
			
				})
				// })
			},
			
			delcircle(item) {
				uni.showModal({
					title: '提示',
					content: '删除该朋友圈?',
					success: res => {
						if (res.cancel) return
			
						uni.request({
							url: this.$api.baseUrl + '/api/common/post/delete',
							header: {
								'content-type': 'application/json'
							},
							data: {
								id: item.id,
							},
							method: 'POST',
							success: res => {
								console.log(res);
								uni.hideLoading()
								// this.list = res.data;
			
								if (res.data.code == 0) {
									let index = this.list.findIndex(s_item => s_item.id == item.id)
									if (index >= 0) {
										this.list.splice(index, 1);
									}
									this.$forceUpdate();
								}
							},
							fail: res => {
								console.log(res);
							}
			
						})
					}
				})
			
			
			
			
			},
			delcomment(item,circle_id) {
				this.comment_info = item;
				thos.circle_id = circle_id;
				this.comment_info = item;
				uni.showModal({
					title: '提示',
					content: '删除该评论?',
					success: res => {
						if (res.cancel) return
			
						uni.request({
							url: this.$api.baseUrl + '/api/common/post_comment/delete',
							header: {
								'content-type': 'application/json'
							},
							data: {
								id: item.id,
							},
							method: 'POST',
							success: res => {
								console.log(res);
								uni.hideLoading()
								// this.list = res.data;
			
								if (res.data.code == 0) {
									this.getcomment()
									this.$forceUpdate();
								}
							},
							fail: res => {
								console.log(res);
							}
			
						})
					}
				})
			
			
			
			
			},
			// changeSpeak(e) {
			// 	console.log('change', e);
			// },
			confirmSpeak(e) {
			
				console.log(this.valueSpeak);
				uni.request({
					url: this.$api.baseUrl + '/api/common/post_comment/add',
					header: {
						'content-type': 'application/json'
					},
					data: {
						post_id: this.comment_info.id,
						user_id: uni.getStorageSync('userId'),
						content: this.valueSpeak
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						if (res.data.code == 0) {
			
			
							this.getcomment();
			
							this.valueSpeak1 = this.valueSpeak
							this.commentShow = false
						}
						// this.list = res.data;
					},
					fail: res => {
						console.log(res);
					}
			
				})
			},
			
			update() {
				console.log(this.info.is_private);
				
				uni.request({
					url: this.$api.baseUrl + '/api/common/post/update',
					header: {
						'content-type': 'application/json'
					},
					data: {
						user_id: uni.getStorageSync('userId'),
						content: this.info.content,
						is_private: Number(this.valueScope),
						pictures: JSON.stringify(this.info.pictures),
						id: this.info.id,
					},
					method: 'POST',
					success: res => {
						console.log(res);
						uni.hideLoading()
						if (res.data.code == 0) {
			
			
							
							this.updateMask = false
						}
						// this.list = res.data;
					},
					fail: res => {
						console.log(res);
					}
			
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
	}

	.status_bar {
		height: 130upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 70upx;
		//background-image: url(../../static/image/banner@3x.png);
		background-size: 100%;
	}
	.scroll-view-box {
		width: 100%;
		background: #fff;
		border-radius: 40upx;
		// position: absolute;
		// top: 22%;
		// left: 0%;
		// z-index: 1;
		background: #fff;
		//margin-top: 40upx; // border-radius: 30upx;
	}

	.u-page {
		padding: 50upx;
		//border-bottom: 10upx solid #f3f6fd;
		.album-container{
			margin-top: 10upx;
		}
		.name-img {
			width: 110upx;
			height: 110upx;
			border-radius: 10upx;
			background: #d54827;
			font-size: 36upx;
			text-align: center;
			line-height: 110upx;
			color: #fff;
			margin-left: -10upx;
			margin-top: -10upx;
		}

		.album__avatar {
			background: #fff
		}

		.album__content {
			display: flex;
			justify-content: space-between;
			align-items: center
		}

		.icon-box {
			display: flex;
			margin-top: 26upx;

		}

		.speak-box {
			background: #f8f9fb;
			// min-height: 70upx;
			// line-height: 70upx;
			// padding-left: 20upx;
			padding: 20upx;
			border-radius: 20upx;
			margin-top: 20upx;
		}

		.btn-enter {
			position: absolute;
			top: 15upx;
			right: 10upx;
		}
	}

	.album {
		@include flex;
		align-items: flex-start;

		&__avatar {
			background-color: $u-bg-color;
			padding: 5px;
			border-radius: 3px;
		}

		&__content {
			margin-left: 10px;
			flex: 1;
		}
	}

	::v-deep.u-input--radius {
		background: #f8f9fb;
	}
	
	.con-shop {
		width: 92%;
		margin-left: 4%;
		height: 100upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.mask-btn {
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-bottom: 20upx;
	
		.mask-btn-left {
			padding: 30upx 130upx;
			background: #eff8ff;
			border-radius: 20upx;
			color: #366dca;
		}
	
		.mask-btn-right {
			padding: 30upx 100upx;
			background: #1c79f2;
			border-radius: 20upx;
			color: #fff;
		}
	}
</style>